BUILD_DIR = ./build		#存放生成的程序的文件夹
ENTRY_POINT = 0xc0001500	#内核起始地址
AS = nasm
CC = gcc
LD = ld
# main.c所需要的头文件
HEAD = 	lib/kernel/print.h\
		lib/stdint.h\
		kernel/init.h\
# init.c所需要的头文件
INIT_HEAD =	kernel/init.h \
			lib/kernel/print.h \
        	lib/stdint.h \
			kernel/interrupt.h \
			device/timer.h
# interrupt.c所需要的头文件
INTERRUPT_HEAD =	kernel/interrupt.h \
        			lib/kernel/print.h \
					lib/stdint.h \
					kernel/global.h \
					lib/kernel/io.h 
# timer.c所需要的头文件
TIMER_HEAD =	device/timer.h \
				lib/stdint.h \
        		lib/kernel/io.h \
				lib/kernel/print.h
# debug.c所需要的头文件
DEBUG_HEAD =	kernel/debug.h \
        		lib/kernel/print.h \
				lib/stdint.h \
				kernel/interrupt.h
# string.c所需要的头文件
STRING_HEAD =	lib/string.h \
				lib/stdint.h kernel/global.h \
				lib/stdint.h \
				kernel/debug.h
# bitmap.c所需要的头文件
BITMAP_HEAD =	lib/kernel/bitmap.h \
    			kernel/global.h \
				lib/stdint.h \
				lib/string.h \
				lib/stdint.h \
     			lib/kernel/print.h \
				kernel/interrupt.h \
				kernel/debug.h
# memory.c的头文件
MEMORY_HEAD =	kernel/memory.h \
				lib/stdint.h lib/kernel/bitmap.h \
   				kernel/global.h \
				kernel/global.h \
				kernel/debug.h \
				lib/kernel/print.h \
				lib/kernel/io.h \
				kernel/interrupt.h \
				lib/string.h \
				lib/stdint.h
# thread.c的头文件
THREAD_HEAD =	thread/thread.h \
				lib/stdint.h \
        		kernel/global.h \
				lib/kernel/bitmap.h \
				kernel/memory.h \
				lib/string.h \
        		lib/stdint.h \
				lib/kernel/print.h \
				kernel/interrupt.h \
				kernel/debug.h
# 文件的所有相对路径目录
LIB = 	-I lib/ \
		-I lib/kernel/ \
		-I lib/user/ \
		-I kernel/ \
		-I device/ \
		-I thread/
ASFLAGS = -f elf
CFLAGS = -Wall $(LIB) -c -fno-builtin -W -Wstrict-prototypes \
         -Wmissing-prototypes 
LDFLAGS = -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
# LDFLAGS = -Ttext $(ENTRY_POINT) -e main #在没构建内存map使用

# 所有生成的目标文件
OBJS = 	$(BUILD_DIR)/main.o \
		$(BUILD_DIR)/print.o \
		$(BUILD_DIR)/init.o \
		$(BUILD_DIR)/interrupt.o \
		$(BUILD_DIR)/kernel.o \
		$(BUILD_DIR)/timer.o \
		$(BUILD_DIR)/debug.o \
		$(BUILD_DIR)/string.o \
		$(BUILD_DIR)/bitmap.o \
		$(BUILD_DIR)/memory.o \
		$(BUILD_DIR)/thread.o
		



.PHONY :clean all mk_dir


all: mk_dir write_all

# build目录用于管理生成文件，不存在则创建
# tmp目录用于挂载img软盘，便于文件拷贝
mk_dir:
	@echo ""
	@echo "init dir:"
	@echo "---------------"
	if [ ! -d $(BUILD_DIR) ];then mkdir $(BUILD_DIR);fi
	if [ ! -d tmp ];then mkdir tmp;fi
	@echo "---------------"



##############   内核编译   ##################

# C语言的编译 #
#1
$(BUILD_DIR)/main.o: kernel/main.c $(HEAD)
	$(CC) $(CFLAGS) $< -o $@
#2
$(BUILD_DIR)/init.o: kernel/init.c $(INIT_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#3
$(BUILD_DIR)/interrupt.o: kernel/interrupt.c $(INTERRUPT_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#4
$(BUILD_DIR)/timer.o: device/timer.c $(TIMER_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#5
$(BUILD_DIR)/debug.o: kernel/debug.c $(DEBUG_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#6
$(BUILD_DIR)/string.o: lib/string.c $(STRING_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#7
$(BUILD_DIR)/bitmap.o: lib/kernel/bitmap.c $(BITMAP_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#8
$(BUILD_DIR)/memory.o: kernel/memory.c $(MEMORY_HEAD)
	$(CC) $(CFLAGS) $< -o $@
#9
$(BUILD_DIR)/thread.o: thread/thread.c $(THREAD_HEAD)
	$(CC) $(CFLAGS) $< -o $@

# 汇编的编译 #
#1
$(BUILD_DIR)/kernel.o: kernel/kernel.S
	$(AS) $(ASFLAGS) $< -o $@
#2
$(BUILD_DIR)/print.o: lib/kernel/print.S
	$(AS) $(ASFLAGS) $< -o $@

############################################


##############   内核链接   ##################
$(BUILD_DIR)/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@
############################################


##############   boot编译   #################
$(BUILD_DIR)/boot.bin: boot/mbr.S
	nasm -I boot/include $< -o $@

$(BUILD_DIR)/loader.bin: boot/loader.S
	nasm -I boot/include $< -o $@

compile_boot: $(BUILD_DIR)/boot.bin $(BUILD_DIR)/loader.bin
############################################


##############   软盘操作   #################
bximg:
	bximage -mode=create -fd=1.44M -q boot.img

check_tmp_dir:
	if [ ! -d tmp ];then mkdir tmp;fi

write_mbr: $(BUILD_DIR)/boot.bin
	dd if=$(BUILD_DIR)/boot.bin of=boot.img bs=512 count=1 conv=notrunc

write_loader: $(BUILD_DIR)/loader.bin
	sudo mount boot.img tmp -t vfat -o loop
	sudo cp $(BUILD_DIR)/loader.bin tmp 
	sync
	sudo umount tmp

write_kernel: $(BUILD_DIR)/kernel.bin
	sudo mount boot.img tmp -t vfat -o loop
	sudo cp $(BUILD_DIR)/kernel.bin tmp 
	sync
	sudo umount tmp

write_all: write_mbr write_loader write_kernel
############################################


clean:
# 删除找不到的文件夹，可能会删除根目录，用cd的方式可以有效防止
	cd $(BUILD_DIR) && rm -f ./*


help:
	@echo ""
	@echo "please input 'make mk_dir': init dir:"
	@echo ""
	@echo "please input 'make compile_boot': create boot.bin & loader.bin"
	@echo ""
	@echo "please input 'make write_kernel': create kernel.bin and write"
	@echo ""
	@echo "please input 'make write_all': compile and write all files to img"
	@echo ""